package com.mtk.launcher.power.view;


import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mtk.launcher.R;
import com.mtk.launcher.power.listener.WeekChooiceListener;


public class WeekView extends RelativeLayout implements View.OnClickListener {

    public WeekView(Context context) {
        this(context, null);
    }

    public WeekView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WeekView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        view = LayoutInflater.from(context).inflate(R.layout.view_week, null);
        addView(view);
        initView();
    }

    View view;
    TextView tv_one, tv_two, tv_thre, tv_four, tv_five, tv_six, tv_sun;

    private void initView() {
        tv_one = view.findViewById(R.id.tv_one);
        tv_two = view.findViewById(R.id.tv_two);
        tv_thre = view.findViewById(R.id.tv_thre);
        tv_four = view.findViewById(R.id.tv_four);
        tv_five = view.findViewById(R.id.tv_five);
        tv_six = view.findViewById(R.id.tv_six);
        tv_sun = view.findViewById(R.id.tv_sun);

        tv_one.setOnClickListener(this);
        tv_two.setOnClickListener(this);
        tv_thre.setOnClickListener(this);
        tv_four.setOnClickListener(this);
        tv_five.setOnClickListener(this);
        tv_six.setOnClickListener(this);
        tv_sun.setOnClickListener(this);
    }

    private void updateViewStatus() {
        updateViewStatus(selectOne, selectTwo, selectThre, selectFour, selectfive, selectSta, selectsun);
    }

    public void updateViewStatus(boolean one, boolean two, boolean three, boolean four, boolean five, boolean six, boolean seven) {
        tv_one.setBackgroundResource(one ? R.drawable.rect_circle_blue_redio : R.drawable.rect_circle_gray_circle);
        tv_two.setBackgroundResource(two ? R.drawable.rect_circle_blue_redio : R.drawable.rect_circle_gray_circle);
        tv_thre.setBackgroundResource(three ? R.drawable.rect_circle_blue_redio : R.drawable.rect_circle_gray_circle);
        tv_four.setBackgroundResource(four ? R.drawable.rect_circle_blue_redio : R.drawable.rect_circle_gray_circle);
        tv_five.setBackgroundResource(five ? R.drawable.rect_circle_blue_redio : R.drawable.rect_circle_gray_circle);
        tv_six.setBackgroundResource(six ? R.drawable.rect_circle_blue_redio : R.drawable.rect_circle_gray_circle);
        tv_sun.setBackgroundResource(seven ? R.drawable.rect_circle_blue_redio : R.drawable.rect_circle_gray_circle);
        if (weekChooiceListener != null) {
            weekChooiceListener.backWeekInfo(one, two, three, four, five, six, seven);
        }
    }

    boolean selectOne = true;
    boolean selectTwo = true;
    boolean selectThre = true;
    boolean selectFour = true;
    boolean selectfive = true;
    boolean selectSta = true;
    boolean selectsun = true;

    @Override
    public void onClick(View v) {
        if (!clickEnable) {
            return;
        }
        if (v.getId() == R.id.tv_one) {
            selectOne = !selectOne;
        }
        if (v.getId() == R.id.tv_two) {
            selectTwo = !selectTwo;
        }
        if (v.getId() == R.id.tv_thre) {
            selectThre = !selectThre;
        }
        if (v.getId() == R.id.tv_four) {
            selectFour = !selectFour;
        }
        if (v.getId() == R.id.tv_five) {
            selectfive = !selectfive;
        }
        if (v.getId() == R.id.tv_six) {
            selectSta = !selectSta;
        }
        if (v.getId() == R.id.tv_sun) {
            selectsun = !selectsun;
        }
        updateViewStatus();
    }


    WeekChooiceListener weekChooiceListener;

    public void setWeekInfo(WeekChooiceListener weekChooiceListener) {
        this.weekChooiceListener = weekChooiceListener;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (changed) {
            layout(l, t, r, b);
        }
    }


    boolean clickEnable = true;

    /***
     * 设置按钮是否可以点击
     * @param b
     */
    public void setClickEnable(boolean b) {
        clickEnable = b;
    }
}
